package com.javasoft.service.YZX.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasoft.dao.YZX.DataDictionaryTypeDao;
import com.javasoft.entity.YZX.DataDictionaryType;
import com.javasoft.entity.ZSW.DataDictionaryEntity;
import com.javasoft.service.YZX._interface.DataDictionaryTypeService;
import com.javasoft.service.ZSW.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Service("dataDictionaryTypeService")
public class DataDictionaryTypeServiceImpl extends ServiceImpl<DataDictionaryTypeDao, DataDictionaryType> implements DataDictionaryTypeService {


    @Autowired
    private DataDictionaryService dataDictionaryService;





    @Override
    @Transactional // 确保两个操作在一个事务中
    public Integer add(DataDictionaryType datadictionaryType) {
        // 首先保存到 datadictionarytype 表
        boolean success = super.save(datadictionaryType);
        if (!success) {
            return 0;
        }

        // 准备要插入/更新到 datadictionary 表的数据
        DataDictionaryEntity dataDictionaryEntity = new DataDictionaryEntity();
        dataDictionaryEntity.setFixedtype(datadictionaryType.getFixedType());
        dataDictionaryEntity.setFixedtypenumber(datadictionaryType.getFixedTypeNumber());
        dataDictionaryEntity.setCreateby(datadictionaryType.getCreateBy());

        // 尝试更新 datadictionary 表；如果记录不存在则插入
        String updateResult = dataDictionaryService.update(dataDictionaryEntity);

        // 检查更新结果是否表示成功
        if ("新增操作成功".equals(updateResult) || "更新操作成功".equals(updateResult)) {
            return 1; // 或者返回其他成功的标志
        } else {
            throw new RuntimeException("Failed to update/insert into datadictionary: " + updateResult);
        }
    }










    @Override
    public List<DataDictionaryType> findByfixedType(String fixedType) {
        QueryWrapper<DataDictionaryType> query = new QueryWrapper<>();
        if (fixedType != null && !fixedType.isEmpty()) {
            query.eq("fixed_type", fixedType);
        }
        return baseMapper.selectList(query);
    }

    @Override
    public Integer deleteById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Integer update(DataDictionaryType datadictionaryType) {
        return baseMapper.updateById(datadictionaryType);
    }



    @Override
    public IPage<DataDictionaryType> pageQuery(int pageNum, int pageSize, String fixedType) {
        Page<DataDictionaryType> page = new Page<>(pageNum, pageSize);
        QueryWrapper<DataDictionaryType> queryWrapper = new QueryWrapper<>();
        if (fixedType != null && !fixedType.isEmpty()) {
            queryWrapper.eq("fixed_type", fixedType);
        }
        return baseMapper.selectPage(page, queryWrapper);
    }




    @Override
    public boolean insertIntoDataDictionary(DataDictionaryType dataDictionaryType) {
        DataDictionaryEntity entity = new DataDictionaryEntity();
        entity.setFixedtype(dataDictionaryType.getFixedType());
        entity.setFixedtypenumber(dataDictionaryType.getFixedTypeNumber()); // 直接使用 Long 类型

        // 调用 datadictionary 的 add 方法进行插入
        return dataDictionaryService.add(entity) > 0;
    }






}